package com.tl.boot.common.utils;

/**
 * com.tl.boot.common.utils -- LogUtil
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/17 9:26
 */

import com.tl.boot.common.wrapper.DecorateHttpServletRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zzq
 */
public class LogUtil {
    private static final Logger log = LoggerFactory.getLogger(LogUtil.class);
    private static final String CALLER_FORMAT = "%s:%s [%s]";

    public static void info(DecorateHttpServletRequestWrapper requestWrapper, String response, Long duration) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());

        log.info("{} >>> {} >>> {} >>> {} >>> {} >>> {} >>> {} >>> {}",
                callerLog, requestWrapper.getIP(), requestWrapper.getMethod(), requestWrapper.getRequestURI(),
                requestWrapper.getHeaders().toJSONString(), requestWrapper.getParams(), response, duration);
    }

    public static void warn(DecorateHttpServletRequestWrapper requestWrapper, String response, Long duration) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());

        log.warn("{} >>> {} >>> {} >>> {} >>> {} >>> {} >>> {} >>> {}",
                callerLog, requestWrapper.getIP(), requestWrapper.getMethod(), requestWrapper.getRequestURI(),
                requestWrapper.getHeaders().toJSONString(), requestWrapper.getParams(), response, duration);
    }

    public static void error(DecorateHttpServletRequestWrapper requestWrapper, String response, Long duration, Exception e) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());

        log.error("{} >>> {} >>> {} >>> {} >>> {} >>> {} >>> {} >>> {} >>> ",
                callerLog, requestWrapper.getIP(), requestWrapper.getMethod(), requestWrapper.getRequestURI(),
                requestWrapper.getHeaders().toJSONString(), requestWrapper.getParams(), response, duration, e);
    }

    public static void info(String logContent) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());
        log.info("{} >>> for developer >>> {}", callerLog, logContent);
    }

    public static void error(Exception e) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());
        log.error("{} >>> for developer >>> ", callerLog, e);
    }

    public static void error(String message) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());
        log.error("{} >>> for developer >>> {}", callerLog, message);
    }
    public static void debug(String message) {
        StackTraceElement[] stack = (new Throwable()).getStackTrace();
        String callerLog = String.format(CALLER_FORMAT, stack[1].getClassName(), stack[1].getMethodName(), stack[1].getLineNumber());
        log.debug("{} >>> for developer >>> {}", callerLog, message);
    }
}

